<?php

namespace App\Http\Models\Log;

use App\Http\Models\Log\Log;
use App\Common\Utils\Common;
use App\Common\Guzzle\GuzzleClient;
use Illuminate\Support\Facades\Auth;

/**
 * LogService 本类属于服务所有逻辑关系
 *
 * index() 日志列表
 * show() 日志查看
 *
 * @author 金飞虎
 * @date 2018/4/5
 * * */
class LogService extends Log
{
    private static $instance;

    public static function log()
    {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function index($request)
    {
        return Log::orderBy('created_at', 'desc')
                        ->where('name', 'like', '%' . $request->name . '%')
                        ->paginate(10);
    }

    public function show($id)
    {
        return Log::where('id', decryptUrl($id))->first();
    }

    public function create($content)
    {
        $ip = Common::getIp();
        $url='http://ip.taobao.com/service/getIpInfo.php?ip='.$ip;
        $region=GuzzleClient::get()->seedRequest('GET',$url);
        $regionData = json_decode($region, true);
        $data = [
            'name' => Auth::user()->name,
            'username' => Auth::user()->username,
            'content' => $content,
            'ip' => Common::getIp(),
            'region' => $regionData['data']['region'] . "-" . $regionData['data']['city']
        ];
        return Log::create($data);
    }
}
